/**
 * Copyright 2013-2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */	  

package com.cos.controller.admin.dynamic;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cos.controller.base.BaseController;
import com.cos.entity.Page;
import com.cos.entity.user.UserDynamic;
import com.cos.service.admin.dynamic.UserDynamicService;
import com.cos.util.AppUtil;
import com.cos.util.Const;
import com.cos.util.Jurisdiction;
import com.cos.util.ObjectExcelView;
import com.cos.util.PageData;

/**
 * TODO 用户动态
 * <p/>
 * 
 * @author <a href="mailto:709920087@qq.com">lxj</a>
 * @version  Date: 2016年3月16日 下午2:30:27
 * @serial 1.0
 * @since 2016年3月16日 下午2:30:27
 */

@Controller
@RequestMapping(value="/userdynamic")
public class UserDynamicController extends BaseController{
	String menuUrl = "userdynamic/list.do";//菜单地址(权限用)
	
	@Resource(name="userDynamicService")
	private UserDynamicService userDynamicService;
	
	/**
	 * save()保存
	 * @param userDynamic
	 * @return 
	 * @throws Exception
	 */
	@RequestMapping(value="save")
    	public Object save(UserDynamic userDynamic) throws Exception{
		logBefore(logger, "UserDynamic");
		PageData pd = new PageData();
		Map<String,String> map = new HashMap<String,String>();
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){
			pd.put("ID", this.get32UUID());
			pd.put("DT_CONTENT",userDynamic.getDT_CONTENT());
			pd.put("DT_TIME",userDynamic.getDT_TIME());
			pd.put("IMG_ID",userDynamic.getIMG_ID());
			pd.put("DT_MAIN_TITLE",userDynamic.getDT_MAIN_TITLE());
			pd.put("DT_ASSIST_TITLE",userDynamic.getDT_ASSIST_TITLE());
			pd.put("U_ID",userDynamic.getU_ID());
			pd.put("DT_ORDER",userDynamic.getDT_ORDER());  
			pd.put("IS_RECOMMEND",userDynamic.getIS_RECOMMEND());  
			pd.put("TAG_ID",userDynamic.getTAG_ID());  
			pd.put("SUPPORT_NUM",userDynamic.getSUPPORT_NUM());  
			pd.put("TRAMPLE_NUM",userDynamic.getTRAMPLE_NUM()); 
			userDynamicService.save(pd); 
			  
		}
		map.put("result", "ok");
		return AppUtil.returnObject(pd, map);
	}
	
	
	@RequestMapping(value="/delete")
	public void delete(PrintWriter out){
		logBefore(logger,"删除用户动态标签");
		PageData pd = new PageData();
		try{
			if(Jurisdiction.buttonJurisdiction(menuUrl,"del")){
				pd = this.getPageData();
				userDynamicService.delete(pd);
			}
			out.write("success");
			out.close();
		}catch(Exception e){
			logger.error(e.toString(), e);
		}
	}
	
	
	/**
	 * 修改用户动态
	 * @param request
	 * @param ID
	 * @param CONTENT
	 * @param TIME
	 * @param IMG_ID
	 * @param MAIN_TITLE
	 * @param ASSIST_TITLE
	 * @param U_ID
	 * @param ORDER
	 * @param IS_RECOMMEND
	 * @param TAG_ID
	 * @param SUPPORT_NUM
	 * @param TRAMPLE_NUM
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="edit")
	public ModelAndView edit(HttpServletRequest request,	
			@RequestParam(value="ID",required=false) String ID,
			@RequestParam(value="CONTENT",required=false) String CONTENT,
			@RequestParam(value="TIME",required=false) String TIME,
			@RequestParam(value="IMG_ID",required=false) String  IMG_ID ,
			@RequestParam(value="MAIN_TITLE",required=false) String  MAIN_TITLE ,
			@RequestParam(value="ASSIST_TITLE",required=false) String  ASSIST_TITLE,
			@RequestParam(value="U_ID",required=false) String  U_ID,
			@RequestParam(value="ORDER",required=false) String  ORDER,
			@RequestParam(value="IS_RECOMMEND",required=false) String  IS_RECOMMEND,
			@RequestParam(value="TAG_ID",required=false) String  TAG_ID,
			@RequestParam(value="SUPPORT_NUM",required=false) String  SUPPORT_NUM,
			@RequestParam(value="TRAMPLE_NUM",required=false) String  TRAMPLE_NUM) throws Exception{
		logBefore(logger, "修改用户动态");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		if(Jurisdiction.buttonJurisdiction(menuUrl, "edit")){
			pd.put("ID",ID);
			pd.put("CONTENT",CONTENT);
			pd.put("TIME",TIME);
			pd.put("IMG_ID",IMG_ID);
			pd.put("MAIN_TITLE",MAIN_TITLE);
			pd.put("ASSIST_TITLE",ASSIST_TITLE);
			pd.put("U_ID",U_ID);
			pd.put("ORDER",ORDER);  
			pd.put("IS_RECOMMEND",IS_RECOMMEND);  
			pd.put("TAG_ID",TAG_ID);  
			pd.put("SUPPORT_NUM",SUPPORT_NUM);  
			pd.put("TRAMPLE_NUM",TRAMPLE_NUM); 
			userDynamicService.edit(pd);
		}
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
		
	}
	
	/**
	 * 去新增页面
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd() throws UnsupportedEncodingException{
		logBefore(logger, "去新增用户动态标签页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("admin/dynamic/dynamicTag_add");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	/**
	 * 去修改页面
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit() throws UnsupportedEncodingException{
		logBefore(logger, "去修改Pictures页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = userDynamicService.findById(pd);	//根据ID读取
			mv.setViewName("admin/dynamic/dynamicTag_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		logBefore(logger, "批量删除DYNAMIC");
		PageData pd = new PageData();		
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			if(Jurisdiction.buttonJurisdiction(menuUrl, "del")){
				List<PageData> pdList = new ArrayList<PageData>();
				String DATA_IDS = pd.getString("DATA_IDS");
				if(null != DATA_IDS && !"".equals(DATA_IDS)){
					String ArrayDATA_IDS[] = DATA_IDS.split(",");
					userDynamicService.deleteAll(ArrayDATA_IDS);
					pd.put("msg", "ok");
				}else{
					pd.put("msg", "no");
				}
				pdList.add(pd);
				map.put("list", pdList);
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	
	/*
	 * 导出到excel
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel() throws UnsupportedEncodingException{
		logBefore(logger, "导出dynamictag到excel");
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try{
			Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
			titles.add("标签名");	//1
			titles.add("标签图标URL");	//2
			titles.add("标签状态");	//3
			titles.add("企业ID");	//4
			titles.add("类型ID");	//5
			dataMap.put("titles", titles);
			List<PageData> varOList = userDynamicService.listAll(pd);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<varOList.size();i++){
				PageData vpd = new PageData();
				vpd.put("var1", varOList.get(i).getString("NAME"));	//1
				vpd.put("var2", varOList.get(i).getString("ICON"));	//2
				vpd.put("var3", varOList.get(i).getString("STATUS"));	//3
				vpd.put("var4", varOList.get(i).getString("C_ID"));	//4
				vpd.put("var5", varOList.get(i).getString("TYPE_ID"));	//5
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ObjectExcelView erv = new ObjectExcelView();
			mv = new ModelAndView(erv,dataMap);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		System.out.println("------------------------");
		logBefore(logger, "列表UserDynamicTag");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			page.setPd(pd);
			List<PageData>	varList = userDynamicService.list(page);	//列出列表
			mv.setViewName("admin/dynamic/userDynamic_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
}
